package beautiful.butterfly.drds.data_exchange.report;

import beautiful.butterfly.drds.data_exchange.statistics.PerfTrace;
import beautiful.butterfly.drds.data_exchange.util.Strings;

import java.text.DecimalFormat;

/**
 * Created by ibm on 2018/9/7.
 */
public class Stringify
{
    private final static DecimalFormat df = new DecimalFormat("0.00");

    public static String getSnapshot(final Message message)
    {
        StringBuilder sb = new StringBuilder();
        sb.append("Total ");
        sb.append(getTotal(message));
        sb.append(" | ");
        sb.append("Speed ");
        sb.append(getSpeed(message));
        sb.append(" | ");
        sb.append("Error ");
        sb.append(getError(message));
        sb.append(" | ");
        sb.append(" All task WaitWriterTime ");
        sb.append(PerfTrace.unitTime(message.getLong(Messages.wait_writer_time)));
        sb.append(" | ");
        sb.append(" All task WaitReaderTime ");
        sb.append(PerfTrace.unitTime(message.getLong(Messages.wait_reader_time)));
        sb.append(" | ");
        if (message.getLong(Messages.transformer_used_time) > 0
                || message.getLong(Messages.transformer_succeed_records) > 0
                || message.getLong(Messages.transformer_failed_records) > 0
                || message.getLong(Messages.transformer_filter_records) > 0)
        {
            sb.append("Transfermor Success ");
            sb.append(String.format("%d records", message.getLong(Messages.transformer_succeed_records)));
            sb.append(" | ");
            sb.append("Transformer Error ");
            sb.append(String.format("%d records", message.getLong(Messages.transformer_failed_records)));
            sb.append(" | ");
            sb.append("Transformer Filter ");
            sb.append(String.format("%d records", message.getLong(Messages.transformer_filter_records)));
            sb.append(" | ");
            sb.append("Transformer usedTime ");
            sb.append(PerfTrace.unitTime(message.getLong(Messages.transformer_used_time)));
            sb.append(" | ");
        }
        sb.append("Percentage ");
        sb.append(getPercentage(message));
        return sb.toString();
    }

    private static String getTotal(final Message message)
    {
        return String.format("%d records, %d bytes",
                message.getLong(Messages.total_read_records),
                message.getLong(Messages.total_read_bytes));
    }

    private static String getSpeed(final Message message)
    {
        return String.format("%s/s, %d records/s",
                Strings.stringify(message.getLong(Messages.byte_speed)),
                message.getLong(Messages.record_speed));
    }

    private static String getError(final Message message)
    {
        return String.format("%d records, %d bytes",
                message.getLong(Messages.total_error_records),
                message.getLong(Messages.total_error_bytes));
    }

    private static String getPercentage(final Message message)
    {
        return df.format(message.getDouble(Messages.percentage) * 100) + "%";
    }
}
